/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hisense.settings.picture;
import com.hisense.settings.util.Wrapper;

import android.content.ContentResolver;
import android.content.Context;
import android.database.ContentObserver;
import android.os.Bundle;
import android.os.Handler;
import android.os.IPowerManager;
import android.os.Parcel;
import android.os.Parcelable;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.preference.SeekBarPreference;
import android.preference.SeekBarDialogPreference;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.SeekBar;
import android.util.Log;
import android.view.KeyEvent;
import com.android.settings.R;

public class ClockPreference extends SeekBarDialogPreference implements
        SeekBar.OnSeekBarChangeListener{

	private static final String TAG = "ClockPreference";
	private static final String KEY_CLOCK_RANGE = "Clock";
    private SeekBar mSeekBar;
	private TextView mTextMin;
	private TextView mTextMax;

    private int mOldClock;
	private TextView mClockNumView;
    private boolean mRestoredClockOldState;

    // clock range is from -50 - 50. Need to make sure that user
    // doesn't set the clock to 0 and get stuck
    //private static final int MINIMUM_CLOCK = -50;
    //private static final int MAXIMUM_CLOCK = 50;

	private int MINIMUM_CLOCK = -50;
    private int MAXIMUM_CLOCK = 50;

    private ContentObserver mClockObserver = new ContentObserver(new Handler()) {
        @Override
        public void onChange(boolean selfChange) {
            //onClockChanged();
        }
    };


    public ClockPreference(Context context, AttributeSet attrs) {
        super(context, attrs);

        setDialogLayoutResource(R.layout.preference_dialog_clock);
        setDialogIcon(R.drawable.ic_settings_display);
    }

    @Override
    protected void showDialog(Bundle state) {
        super.showDialog(state);

        //getContext().getContentResolver().registerContentObserver(
        //        Settings.System.getUriFor(Settings.System.SCREEN_BRIGHTNESS), true,
        //        mBrightnessObserver);

        mRestoredClockOldState = false;
    }

    @Override
    protected void onBindDialogView(View view) {
        super.onBindDialogView(view);

		Bundle bundle = new Bundle();
		bundle = Wrapper.getWrapperInstance().getPropertyRange(KEY_CLOCK_RANGE);
		MAXIMUM_CLOCK = bundle.getInt("max");
		MINIMUM_CLOCK = bundle.getInt("min");

        mSeekBar = (SeekBar)view.findViewById(R.id.clock_seekbar);
		//test 
		//MAXIMUM_CLOCK = 32;
		//MINIMUM_CLOCK = -23;
        mSeekBar.setMax(MAXIMUM_CLOCK- MINIMUM_CLOCK);
		mSeekBar.setKeyProgressIncrement(1);
        mOldClock = getClock();
        mSeekBar.setProgress(mOldClock - MINIMUM_CLOCK);
		Log.d(TAG, "InitClock------>>>>>InitClock" + mOldClock);

        mSeekBar.setOnSeekBarChangeListener(this);
		mClockNumView= (TextView)view.findViewById(R.id.clock_value);
		mClockNumView.setText(""+mOldClock);

		mTextMin= (TextView)view.findViewById(R.id.clock_min);
		mTextMin.setText(""+MINIMUM_CLOCK);
		mTextMax= (TextView)view.findViewById(R.id.clock_max);
		mTextMax.setText(""+MAXIMUM_CLOCK);	
    }

    public void onProgressChanged(SeekBar seekBar, int progress,
            boolean fromTouch) {
        setClock(progress + MINIMUM_CLOCK);
		mClockNumView.setText("" + (progress + MINIMUM_CLOCK));
		
		Bundle bundle = null;
		bundle = new Bundle();
		bundle = Wrapper.getWrapperInstance().getPropertyRange("HorizontalPosition");
		Log.d(TAG, 
		"HorizontalPosition*****max " +bundle.getInt("max") + " min " + bundle.getInt("min"));	
		bundle = Wrapper.getWrapperInstance().getPropertyRange("VerticalPosition");
		Log.d(TAG, "VerticalPosition*****max " 
						+bundle.getInt("max") + " min " + bundle.getInt("min"));	
		bundle = Wrapper.getWrapperInstance().getPropertyRange("Phase");
		Log.d(TAG, "Phase*****max " 
						+bundle.getInt("max") + " min " + bundle.getInt("min"));	
		bundle = Wrapper.getWrapperInstance().getPropertyRange("Clock");
		Log.d(TAG, "Clock*****max " 
						+bundle.getInt("max") + " min " + bundle.getInt("min") + "getClock()" + getClock());		
		bundle = Wrapper.getWrapperInstance().getPropertyRange("HorizontalSize");
		Log.d(TAG, "HorizontalSize*****max " 
						+bundle.getInt("max") + " min " + bundle.getInt("min"));		
		bundle = Wrapper.getWrapperInstance().getPropertyRange("VerticalSize");
		Log.d(TAG, "VerticalSize*****max " 
						+bundle.getInt("max") + " min " + bundle.getInt("min"));
    }

    public void onStartTrackingTouch(SeekBar seekBar) {
        // NA
    }

    public void onStopTrackingTouch(SeekBar seekBar) {
        // NA
    }


    private void setClock(int clock) {
		Wrapper.getWrapperInstance().setClock(clock); 
    }
    private int getClock() {
        int clock = 0;
		clock = Wrapper.getWrapperInstance().getClock();
        return clock;
    }


    private void onClockChanged() {
        int clock = getClock();
        mSeekBar.setProgress(clock);
    }

    @Override
    protected void onDialogClosed(boolean positiveResult) {
        super.onDialogClosed(positiveResult);

        final ContentResolver resolver = getContext().getContentResolver();
        if (positiveResult) {
			
        } else {
            restoreClockOldState();
        }
        callChangeListener(getClock());
        resolver.unregisterContentObserver(mClockObserver);
    }

    private void restoreClockOldState() {
        if (mRestoredClockOldState) return;

        setClock(mOldClock);
        mRestoredClockOldState = true;
    }





    @Override
    protected Parcelable onSaveInstanceState() {
        final Parcelable superState = super.onSaveInstanceState();
        if (getDialog() == null || !getDialog().isShowing()) return superState;

        // Save the dialog state
        final SavedState myState = new SavedState(superState);
        myState.progress = mSeekBar.getProgress();
        myState.oldProgress = mOldClock;

        // Restore the old state when the activity or dialog is being paused
        restoreClockOldState();
        return myState;
    }

    @Override
    protected void onRestoreInstanceState(Parcelable state) {
        if (state == null || !state.getClass().equals(SavedState.class)) {
            // Didn't save state for us in onSaveInstanceState
            super.onRestoreInstanceState(state);
            return;
        }

        SavedState myState = (SavedState) state;
        super.onRestoreInstanceState(myState.getSuperState());
        mOldClock = myState.oldProgress;
        setClock(myState.progress + MINIMUM_CLOCK);
    }

    private static class SavedState extends BaseSavedState {

        int progress;
        int oldProgress;

        public SavedState(Parcel source) {
            super(source);
            progress = source.readInt();
            oldProgress = source.readInt();
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            super.writeToParcel(dest, flags);
            dest.writeInt(progress);
            dest.writeInt(oldProgress);
        }

        public SavedState(Parcelable superState) {
            super(superState);
        }

        public static final Parcelable.Creator<SavedState> CREATOR =
                new Parcelable.Creator<SavedState>() {

            public SavedState createFromParcel(Parcel in) {
                return new SavedState(in);
            }

            public SavedState[] newArray(int size) {
                return new SavedState[size];
            }
        };
    }
}

